package com.detection.dahua.demo.brm.device;

import cn.hutool.json.JSONUtil;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.oauth.utils.HttpUtils;
import com.detection.dahua.config.OauthConfigUtil;
import com.detection.dahua.model.event.eventSubcribe.SubscribeRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AccessControlTest {
    public static String ip="192.168.0.235";
    public static String port="8084";

    /**
     * 订阅报警事件
     */
    public static void subscribeAlarmEvent(){
        SubscribeRequest subscribeRequest = new SubscribeRequest();
        SubscribeRequest.Param param = new SubscribeRequest.Param();

        SubscribeRequest.Param.Subsystem subsystem = new SubscribeRequest.Param.Subsystem();
        subsystem.setName(ip+"_"+port);
        subsystem.setMagic(ip+"_"+port);
        param.setSubsystem(subsystem);

        List<SubscribeRequest.Param.Monitor> monitors = new ArrayList();
        SubscribeRequest.Param.Monitor monitor = new SubscribeRequest.Param.Monitor();
        //回调地址的接口代码可参考SubscribeCallBackController类中的receiveMsg方法
        monitor.setMonitor("http://"+ip+":"+port+"/test/receiveAlarmMsg");

        List<SubscribeRequest.Param.Monitor.Event>events = new ArrayList<>();
        SubscribeRequest.Param.Monitor.Event event = new SubscribeRequest.Param.Monitor.Event();
        //订阅报警事件，填alarm
//        event.setCategory("alarm");
        List<SubscribeRequest.Param.Monitor.Event.Authority> authorities = new ArrayList();
        SubscribeRequest.Param.Monitor.Event.Authority authority=new SubscribeRequest.Param.Monitor.Event.Authority();

        //若订阅指定报警类型可参考下列代码进行修改，若订阅所有报警类型，则把下列types相关代码删除即可
//        List<String> types= new ArrayList<>();
//        types.add("57");
//        types.add("51");
//        types.add("61");
//        authority.setTypes(types);

        //若订阅指定组织可参考下列代码进行修改,若订阅所有组织，则将下列orgs相关代码删除
//        List<String>orgs = new ArrayList<>();
//        orgs.add("001001");
//        orgs.add("001002");
//        authority.setOrgs(orgs);

        //若订阅指定设备和通道可参考下列代码进行修改,若订阅所有设备和通道，则将下列nodeCodes相关代码删除
//        List<String>nodeCodes=new ArrayList();
//        nodeCodes.add("1002511$7$0$0");
//        nodeCodes.add("1002509$7$0$0");
//        authority.setNodeCodes(nodeCodes);

        authorities.add(authority);
        event.setAuthorities(authorities);
        events.add(event);
        monitor.setEvents(events);
        monitors.add(monitor);
        param.setMonitors(monitors);
        subscribeRequest.setParam(param);
        log.info("SubscribeDemo,subscribeAlarmEvent,request:{}", JSONUtil.toJsonStr(subscribeRequest));

        OauthConfigUserPwdInfo config = OauthConfigUtil.getOauthConfig();
        try {
            GeneralResponse response = HttpUtils.executeJson("/evo-apigw/evo-event/1.0.0/subscribe/mqinfo", subscribeRequest,null, Method.POST, config,GeneralResponse.class);
            log.info("SubscribeDemo,subscribeAlarmEvent,response:{}", response.getResult());
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
    }

    public static void main(String[] args) {
        subscribeAlarmEvent();
    }

}
